<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of filter_obj
 *
 * @author Juraj
 */
class filter_obj {

    //put your code here
    public function updateFilter($userId, $table, $filter) {
        $db_obj = new db_obj();

        $userId = (int) $userId;
        if (empty($userId))
            return new error_obj('$userId is empty', get_class(), __FUNCTION__);

        if (empty($table))
            return new error_obj('$table is empty', get_class(), __FUNCTION__);

        /* $sql = "DELETE FROM " . $GLOBALS['tb_filters'] . " WHERE userId='" . $userId . "' AND variable='" . $table . "'";
          base_obj::debug("filter delete sql", $sql);
          $result = $db_obj->query($sql);

          if (error_obj::isError($result))
          return $result;
          else {
          $in_data = array();
          $in_data['userId'] = $userId;
          $in_data['variable'] = $table;
          $in_data['filter'] = json_encode($filter);

          $result = $db_obj->insert($GLOBALS['tb_filters'], $in_data);
          if (error_obj::isError($result))
          return $result;
          } */

        $in_data = array();
        $in_data['userId'] = $userId;
        $in_data['variable'] = $table;
        $in_data['filter'] = json_encode($filter);

        $keys = array('userId' => $userId);
        $keys = array('variable' => $table);
        
        $result = $db_obj->insertUpdate($GLOBALS['tb_filters'], $in_data, $keys);
        
        if (error_obj::isError($result))
            return $result;

        return true;
    }

    public function getFilters($userId) {
        $db_obj = new db_obj();

        $userId = (int) $userId;
        if (empty($userId))
            return new error_obj('$userId is empty', get_class(), __FUNCTION__);

        $sql = "SELECT * FROM " . $GLOBALS['tb_filters'] . " WHERE userId='" . $userId . "'";

        $result = $db_obj->getAll($sql);

        return $result;
    }
    
    public function setClientToEachFilter($client) {
        
        $client = (int) $client;
        
        $filters = array(
            'list_of_tasks_filter',
            'timesheets_filter',
            'list_of_users_filter'
        );
        
        foreach ($filters as $filter){
            if (empty($client))
                unset($_SESSION[$filter]['client']);
            else
                $_SESSION[$filter]['client'] = $client;
        }
        
        return true;
        
    }
    
    public function setDefaultFilterOfListToValue($list, $filter, $value) {
        
        $value = (int) $value;
        
        $_SESSION[$list][$filter] = $value;
        
        return true;
        
    }

}

?>
